package cn.react.lock.message;
import android.os.Handler;
import android.text.TextUtils;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;

import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.mob.MobSDK;
import com.mob.pushsdk.MobPush;
import com.mob.pushsdk.MobPushLocalNotification;
import com.mob.tools.utils.DeviceHelper;
import java.util.Random;


@SuppressWarnings("unused")
public class LockMessageModule extends ReactContextBaseJavaModule{
    private ReactApplicationContext reactContext;
    public static LockMessageModule sModule =null;
    public static String pushTxt = "";

    LockMessageModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.onCreate(reactContext);
    }
    public void onCreate(ReactApplicationContext reactContext)
    {
        this.reactContext = reactContext;
        LockMessageModule.sModule = this;
        System.out.println("LockMessageModule.sModule 1"+LockMessageModule.sModule);
    }
    @Override
    public String getName() {
        return "LockMessageModule";
    }

    @ReactMethod
    public  void submitPolicy()
    {
        MobSDK.submitPolicyGrantResult(true,null);

    }
    public  static void push(String s,int code)
    {
        System.out.println("parseMainPluginPushIntent:" + s);
        System.out.println("LockMessageModule.sModule 2"+LockMessageModule.sModule);
        if(code == 0)
        {
            LockMessageModule.pushTxt = s;
        }
        else
        {
            if(LockMessageModule.sModule!=null)
            {
                LockMessageModule.sModule.pushMsg(s,code);
            }
        }
    }
    public void pushMsg(String s ,int code )
    {
            DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter = this.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
            if(eventEmitter!=null && !("".equals(s))) {
                WritableMap map = Arguments.createMap();
                map.putString("content", s);
                map.putInt("code",1);
                eventEmitter.emit("LockMessageModule", map);
            }
    }
    @ReactMethod
    public void  checkPush()
    {
        DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter = this.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
        if(eventEmitter!=null && !("".equals(LockMessageModule.pushTxt)))
        {
            WritableMap map = Arguments.createMap();
            map.putString("content",LockMessageModule.pushTxt);
            map.putInt("code",0);
            LockMessageModule.pushTxt = "";
            eventEmitter.emit("LockMessageModule",map);
        }
    }
    @ReactMethod
    public  void setAlias(final ReadableMap param)
    {
        String idCode = param.getString("idCode");
        MobPush.setAlias(idCode);
    }
    @ReactMethod
    public void deleteAlias()
    {
        MobPush.deleteAlias();
    }
    @ReactMethod
    public  void clearLocalNotifications()
    {
        MobPush.clearLocalNotifications();
    }
    @ReactMethod
    public void sendMessage(final ReadableMap param) {
        String title = param.getString("title");
        String content = param.getString("content");
        MobPushLocalNotification notification = new MobPushLocalNotification();
        String appName = DeviceHelper.getInstance(this.reactContext).getAppName();
        notification.setTitle(TextUtils.isEmpty(appName) ? title : appName);
        notification.setContent(content);
        //notification.setIcon(R.mipmap.ic_launcher);
        notification.setVoice(true);//可设置不进行声音提醒，默认声音、振动、指示灯
        notification.setNotificationId(new Random().nextInt());
        notification.setTimestamp(System.currentTimeMillis());
        notification.setNotifySound("");
        MobPush.addLocalNotification(notification);
    }
}
